//
//  XYKeybordToolBar.swift
//  SinaWB_XY
//
//  Created by 小熊猫 on 15/10/23.
//  Copyright © 2015年 小熊猫. All rights reserved.
//

import UIKit

enum XYKeybordToolBarType:Int{
    case Recent = 100
    case Default   = 101
    case Emoji   = 102
    case Lxh      = 103
}
//添加代理,实现点击滚动的功能
protocol XYKeybordToolBarDeletage:NSObjectProtocol{
    func didKeybordToolBarButtonWithPage(toolbar:XYKeybordToolBar,type:XYKeybordToolBarType)
}

class XYKeybordToolBar: UIView {
    //选中的按钮
    private var seleButton:XYToolbarButton?
    //代理属性
    var delegate:XYKeybordToolBarDeletage?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setToolBarButton("最近",imageName:"compose_emotion_table_left",type: .Recent)
        setToolBarButton("默认",imageName:"compose_emotion_table_mid",type: .Default)
        setToolBarButton("Emoji",imageName:"compose_emotion_table_mid",type: .Emoji)
        setToolBarButton("浪小花",imageName:"compose_emotion_table_right",type: .Lxh)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加button
    private func setToolBarButton(titleName:String,imageName:String,type:XYKeybordToolBarType){
        let button = XYToolbarButton()
        
        button.tag = type.rawValue
        button.addTarget(self, action: "didToolbarClick:", forControlEvents: UIControlEvents.TouchUpInside)
        //文字
        button.setTitle(titleName, forState: UIControlState.Normal)
        //文字颜色
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Selected)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        //图片
        button.setBackgroundImage(UIImage(named: "\(imageName)_normal"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "\(imageName)_selected"), forState: UIControlState.Selected)
        
        addSubview(button)
    }
    
    //按钮点击事件
    @objc private func didToolbarClick(button:XYToolbarButton){
        self.seleButton?.selected = false
        
        button.selected = true
        
        self.seleButton = button
        
        if let del = self.delegate{
            del.didKeybordToolBarButtonWithPage(self, type: XYKeybordToolBarType(rawValue: button.tag)!)
        }
    }
    
    
    //  传一个type.返回一个按钮并选中
     func seleButtonWithType(type:XYKeybordToolBarType){
        let button = self.viewWithTag(type.rawValue) as! XYToolbarButton
       
        if self.seleButton != button{
            self.seleButton?.selected = false
            button.selected = true
            self.seleButton = button
        }
        
    }
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        //计算宽度
        let chlidButtonW = self.width / CGFloat(subviews.count)
        
        for (index,value) in subviews.enumerate(){
            
            value.size = CGSizeMake(chlidButtonW, self.height)
            value.x = CGFloat(index) * chlidButtonW
            
            if index == 0{
                self.seleButton = value as? XYToolbarButton
                self.seleButton?.selected = true
            }
        }
        
    }

}







